home *** CD-ROM | disk | FTP | other *** search
- #include "GifScan.h"
-
- #include "DSUserProcs.h"
- #include "DSGlobals.h"
- #include "Prefs.h"
-
- #ifndef __BALLOONS__
- #include <Balloons.h>
- #endif
-
- #ifndef __FOLDERS__
- #include <Folders.h>
- #endif
-
- thePrefsHandle gPrefs;
-
- #define kDialogID 129
- #define kOn 1
- #define kOff 0
- #define kActive 0
- #define kInactive 255
- #define kFontsMenu 201
-
- #define SIZEOFRESOURCE 30 // Original size of the prefs data in this version
-
- static Boolean helpOn;
-
- enum theDialogItems{ kShowCheckBox = 3,
- kHexaRadio = 4,
- kDeciRadio = 5,
- kPercRadio = 6,
- kPromptForSave = 8,
- kForgetTheRest = 9,
- kFontSizePopup = 10,
- kFullPath = 12,
- kFontNamePopup = 13,
- kSaveHTMLTagToClip = 15,
- kToggleHelpIcon = 17,
- kToggleHelpCheckBox = 18 };
-
- enum theFontSizesMenu { iNine = 1,
- iTen,
- iTwelve,
- iFourteen,
- iEighteen,
- iTwentyFour };
-
- static pascal ControlRef SnatchHandle(DialogRef theBox, short theGetItem)
- {
- short itemType;
- Rect itemRect;
- Handle theHandle;
-
- GetDialogItem(theBox, theGetItem, &itemType, &theHandle, &itemRect);
- return((ControlRef)theHandle);
- }
-
- static void ActivateRadios(ControlRef ctlHexa, ControlRef ctlDeci, ControlRef ctlPerc)
- {
- HiliteControl(ctlHexa, kActive);
- HiliteControl(ctlDeci, kActive);
- HiliteControl(ctlPerc, kActive);
- }
-
- static void DeActivateRadios(ControlRef ctlHexa, ControlRef ctlDeci, ControlRef ctlPerc)
- {
- HiliteControl(ctlHexa, kInactive);
- HiliteControl(ctlDeci, kInactive);
- HiliteControl(ctlPerc, kInactive);
- }
-
-
- static void SetFontSizePopupItem(ControlRef thePopup)
- {
- switch ( (*gPrefs)->fontSize )
- {
- case 9:
- SetCtlValue(thePopup, iNine);
- break;
- case 10:
- SetCtlValue(thePopup, iTen);
- break;
- case 12:
- SetCtlValue(thePopup, iTwelve);
- break;
- case 14:
- SetCtlValue(thePopup, iFourteen);
- break;
- case 18:
- SetCtlValue(thePopup, iEighteen);
- break;
- case 24:
- SetCtlValue(thePopup, iTwentyFour);
- break;
- default:
- SetCtlValue(thePopup, iNine);
- break;
- }
- }
-
- static short GetFontSizePopup(ControlRef thePopup)
- {
- short item;
-
- item = GetCtlValue(thePopup);
-
- switch (item)
- {
- case iNine:
- return(9);
- break;
- case iTen:
- return(10);
- break;
- case iTwelve:
- return(12);
- break;
- case iFourteen:
- return(14);
- break;
- case iEighteen:
- return(18);
- break;
- case iTwentyFour:
- return(24);
- break;
- default:
- return(9);
- break;
- }
- }
-
- static pascal Boolean TheModalFilterProc(DialogRef dialog, EventRecord *dialogEvent,
- short *itemHit)
- {
- ModalFilterUPP theModalProc;
- Boolean handled = false, wasAKey, cmdDown;
- OSErr err;
- char theKey;
- WindowRef window;
-
- wasAKey = (dialogEvent->what == keyDown) || (dialogEvent->what == autoKey);
-
- // Check if user turned on Balloon help in the menubar.
- helpOn = HMGetBalloons();
- if ( helpOn )
- SetCtlValue(SnatchHandle(dialog, kToggleHelpCheckBox), kOn);
- else
- SetCtlValue(SnatchHandle(dialog, kToggleHelpCheckBox), kOff);
-
- if ( dialogEvent->what == updateEvt )
- DoUpdate((WindowRef)dialogEvent->message);
-
- if ( !handled)
- {
- err = GetStdFilterProc(&theModalProc);
- if (err != noErr) ErrorAlert(kErrStringID, eMemError, err, false);
- handled = theModalProc(dialog, dialogEvent, itemHit);
- }
-
- return(handled);
- }
-
- pascal void SetPreferences(void)
- {
- DialogRef dialog;
- GrafPtr savePort;
- Boolean dialogDone, userCancelled, doShow, doSave, doForget, doFullPath, doCopy;
- short itemHit, newFontSize;
- ControlRef ctlShow, ctlHexa, ctlDeci, ctlPerc, ctlSave, ctlForget, ctlFontSize;
- ControlRef ctlFullPath, ctlFontName, ctlHTMLCopy, ctlHelpCheckBox;
- MenuRef fontMenu;
- Str255 fontString, menuString;
- short menuLength, i, menuIndex;
-
- GetPort(&savePort);
- SetCursor(&qd.arrow);
-
- // Build font menu before dialog loads
- fontMenu = GetMenu(kFontsMenu);
- AddResMenu(fontMenu, 'FONT');
- menuLength = CountMItems(fontMenu);
- GetFontName((*gPrefs)->fontNum, fontString);
- for ( i = 1; i <= menuLength; i++)
- {
- GetMenuItemText(fontMenu, i, menuString);
- if ( !IUEqualString(menuString, fontString) )
- {
- CheckItem(fontMenu, i, true);
- menuIndex = i;
- }
- }
-
- dialog = GetNewDialog(kDialogID, nil, (WindowPtr)-1L);
-
- SetDialogDefaultItem(dialog, ok);
- SetDialogCancelItem(dialog, cancel);
-
- ctlShow = SnatchHandle(dialog, kShowCheckBox);
- ctlHexa = SnatchHandle(dialog, kHexaRadio);
- ctlDeci = SnatchHandle(dialog, kDeciRadio);
- ctlPerc = SnatchHandle(dialog, kPercRadio);
- ctlSave = SnatchHandle(dialog, kPromptForSave);
- ctlForget = SnatchHandle(dialog, kForgetTheRest);
- ctlFontSize = SnatchHandle(dialog, kFontSizePopup);
- ctlFullPath = SnatchHandle(dialog, kFullPath);
- ctlFontName = SnatchHandle(dialog, kFontNamePopup);
- ctlHTMLCopy = SnatchHandle(dialog, kSaveHTMLTagToClip);
- ctlHelpCheckBox = SnatchHandle(dialog, kToggleHelpCheckBox);
-
- SetCtlValue(ctlShow, (*gPrefs)->showColors);
- SetCtlValue(ctlHexa, (*gPrefs)->hexadecimal);
- SetCtlValue(ctlDeci, (*gPrefs)->decimal);
- SetCtlValue(ctlPerc, (*gPrefs)->percentages);
- SetCtlValue(ctlSave, (*gPrefs)->promptSave);
- SetCtlValue(ctlForget, (*gPrefs)->forget);
- SetCtlValue(ctlFullPath, (*gPrefs)->fullPath);
- SetCtlValue(ctlHTMLCopy, (*gPrefs)->copy);
-
- SetFontSizePopupItem(ctlFontSize);
-
- SetCtlValue(ctlFontName, menuIndex);
-
- doShow = (*gPrefs)->showColors;
- doSave = (*gPrefs)->promptSave;
- doForget = (*gPrefs)->forget;
- doFullPath = (*gPrefs)->fullPath;
- doCopy = (*gPrefs)->copy;
-
- helpOn = HMGetBalloons();
- SetCtlValue(ctlHelpCheckBox, helpOn);
-
- if ( doForget )
- {
- HiliteControl(ctlShow, kInactive);
- DeActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- }
- else
- {
- HiliteControl(ctlShow, kActive);
- if ( doShow )
- ActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- else
- DeActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- }
-
- dialogDone = false;
- SetPort(dialog);
- ShowWindow(dialog);
-
- while ( !dialogDone )
- {
- ModalDialog((ModalFilterUPP)TheModalFilterProc, &itemHit);
- switch (itemHit)
- {
- case ok:
- userCancelled = false;
- dialogDone = true;
- break;
-
- case cancel:
- userCancelled = true;
- dialogDone =true;
- break;
-
- case kForgetTheRest:
- doForget = !doForget;
- SetCtlValue(ctlForget, doForget);
- if ( doForget )
- {
- HiliteControl(ctlShow, kInactive);
- DeActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- }
- else
- {
- HiliteControl(ctlShow, kActive);
- if ( doShow )
- ActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- else
- DeActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- }
- break;
-
- case kShowCheckBox:
- doShow = !doShow;
- SetCtlValue(ctlShow, doShow);
- if ( doShow )
- ActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- else
- DeActivateRadios(ctlHexa, ctlDeci, ctlPerc);
- break;
-
- case kHexaRadio:
- SetCtlValue(ctlHexa, kOn);
- SetCtlValue(ctlDeci, kOff);
- SetCtlValue(ctlPerc, kOff);
- break;
-
- case kDeciRadio:
- SetCtlValue(ctlHexa, kOff);
- SetCtlValue(ctlDeci, kOn);
- SetCtlValue(ctlPerc, kOff);
- break;
-
- case kPercRadio:
- SetCtlValue(ctlHexa, kOff);
- SetCtlValue(ctlDeci, kOff);
- SetCtlValue(ctlPerc, kOn);
- break;
-
- case kPromptForSave:
- doSave = !doSave;
- SetCtlValue(ctlSave, doSave);
- break;
-
- case kFullPath:
- doFullPath = !doFullPath;
- SetCtlValue(ctlFullPath, doFullPath);
- break;
-
- case kFontNamePopup:
- // Remove old check item
- for ( i = 1; i <= menuLength; i++ )
- CheckItem(fontMenu, i, false);
- break;
-
- case kSaveHTMLTagToClip:
- doCopy = !doCopy;
- SetCtlValue(ctlHTMLCopy, doCopy);
- break;
-
- case kToggleHelpCheckBox:
- helpOn = !helpOn;
- SetCtlValue(ctlHelpCheckBox, helpOn);
- HMSetBalloons(helpOn);
- break;
- }
- }
-
- if ( !userCancelled )
- {
- (*gPrefs)->showColors = GetCtlValue(ctlShow);
- (*gPrefs)->hexadecimal = GetCtlValue(ctlHexa);
- (*gPrefs)->decimal = GetCtlValue(ctlDeci);
- (*gPrefs)->percentages = GetCtlValue(ctlPerc);
- (*gPrefs)->promptSave = GetCtlValue(ctlSave);
- (*gPrefs)->forget = GetCtlValue(ctlForget);
- (*gPrefs)->fullPath = GetCtlValue(ctlFullPath);
- (*gPrefs)->copy = GetCtlValue(ctlHTMLCopy);
-
- newFontSize = GetFontSizePopup(ctlFontSize);
-
- i = GetCtlValue(ctlFontName);
- GetMenuItemText(fontMenu, i, menuString);
- GetFNum(menuString, &(*gPrefs)->fontNum);
- }
-
- DisposeDialog(dialog);
-
- SetPort(savePort);
-
- if ( !userCancelled )
- {
- if ( newFontSize != (*gPrefs)->fontSize )
- UpdateWindowFontSize(newFontSize);
- (*gPrefs)->fontSize = newFontSize;
- UpdateWindowFont((*gPrefs)->fontNum);
- }
- }
-
- // I am using my own error numbers in these routines, starting at 12900
- pascal short GetPrefsFile(void)
- {
- OSErr err;
- Handle prefResHandle;
- FSSpec prefsFSSpec;
- Str255 prefsFileName;
- StringHandle prefStrHandle;
- short prefRefNum;
- long prefDirID;
- short prefVRefNum;
-
- // Get the name of the prefsfile from a resource
- prefStrHandle = GetString(prefsFileNameRsrc);
- if (prefStrHandle != nil)
- BlockMoveData(*prefStrHandle, prefsFileName, (Size)(**prefStrHandle) + 1);
- else
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12900, false);
-
- // find the prefs folder's volume reference number and directory ID
- err = FindFolder(kOnSystemDisk, kPreferencesFolderType, kCreateFolder, &prefVRefNum, &prefDirID);
- if ( err != noErr ) ErrorAlert(kErrStringID, eCouldNotWriteResources, 12901, false);
-
- // Make an FSSPec for prefsfile
- err = FSMakeFSSpec(prefVRefNum, prefDirID, prefsFileName, &prefsFSSpec);
-
- if ( err == noErr)
- {
- // open the preference file, will be closed in next subroutines
- prefRefNum = FSpOpenResFile(&prefsFSSpec, fsRdWrPerm);
- if (prefRefNum == -1) ErrorAlert(kErrStringID, eCouldNotWriteResources, 12902, false);
- return(prefRefNum);
- }
-
- if ( err == fnfErr )
- {
- // Get resources from open application
- prefResHandle = Get1Resource(kPrefResType, prefsResNum);
- if (prefResHandle == nil) ErrorAlert(kErrStringID, eCouldNotWriteResources, 12903, false);
-
- // prefs file doesn't already exist, so create it
- FSpCreateResFile(&prefsFSSpec, prefsCreator, prefsFileType, smSystemScript);
- if ( ResError() != noErr )
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12904, false);
-
- // open the preference file
- prefRefNum = FSpOpenResFile(&prefsFSSpec, fsRdWrPerm);
- if (prefRefNum == -1)
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12905, false);
-
- DetachResource(prefResHandle);
- AddResource(prefResHandle, kPrefResType, prefsResNum, "\p");
- ChangedResource(prefResHandle);
- if ( ResError() != noErr )
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12906, false);
-
- // Close the file
- CloseResFile(prefRefNum);
- return(prefRefNum);
- }
-
- if ( err != noErr || err != fnfErr)
- {
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12907, false);
- return(-1);
- }
- }
-
- pascal Handle ReadPrefData(void)
- {
- short prefRefNum;
- Handle theData;
- Boolean oldPrefs;
-
- prefRefNum = GetPrefsFile();
- if (prefRefNum != -1)
- {
- UseResFile(prefRefNum);
- theData = GetResource(kPrefResType, prefsResNum);
-
- // Should check for old preferences here and update if neccessary
- if ( (*((thePrefsHandle)(theData)))->version != PREFSVERSION )
- oldPrefs = true;
- else
- oldPrefs = false;
-
- // If prefs are corrupted, remove from file and make a new file
- // Should be with some sort of checksum, I just use the size.
- if ( ( GetHandleSize(theData) != SIZEOFRESOURCE ) || oldPrefs )
- {
- // Remove the old prefs, avoid an error, make sure it exists
- if ( theData != nil )
- {
- RemoveResource(theData);
- DisposeHandle(theData);
- }
-
- // Add the new data to the prefsfile
- theData = NewHandleClear(SIZEOFRESOURCE);
- if ( theData != nil )
- {
- // Get original resource from application
- theData = GetNamedResource(kPrefResType, "\pDefaults");
- if ( ResError() != noErr )
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12909, false);
-
- UseResFile(prefRefNum);
- AddResource(theData, kPrefResType, prefsResNum, "\p");
- ChangedResource(theData);
- WriteResource(theData);
- UpdateResFile(prefRefNum);
- if ( ResError() != noErr )
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12910, false);
-
- // Throw message at user to tell prefs were messed up and renewed
- if ( oldPrefs )
- ErrorAlert(kErrStringID, eNewPrefsVersion, 12911, false);
- else
- ErrorAlert(kErrStringID, eCorruptPrefs, 12912, false);
- }
- else
- // Couldn't allocate memory for a new handle
- ErrorAlert(kErrStringID, eMemError, 12913, true);
- }
-
- DetachResource(theData);
- CloseResFile(prefRefNum);
-
- return(theData);
- }
- else
- return(nil);
- }
-
- pascal Boolean WritePrefData(Handle theData)
- {
- short prefRefNum;
- Handle tempData;
- Boolean succes = true;
-
- prefRefNum = GetPrefsFile();
-
- // Make sure we have a file and data to write to it
- if ( (prefRefNum != -1) && (theData != nil) )
- {
- UseResFile(prefRefNum);
-
- // Remove the old resource with the same number and type
- tempData = Get1Resource(kPrefResType, prefsResNum);
- if ( tempData != nil )
- {
- RemoveResource(tempData);
- DisposeHandle(tempData);
- }
-
- // Add the new data to the prefsfile
- AddResource(theData, kPrefResType, prefsResNum, "\p");
- ChangedResource(theData);
- if ( ResError() != noErr )
- ErrorAlert(kErrStringID, eCouldNotWriteResources, 12914, false);
- CloseResFile(prefRefNum);
- return(succes);
- }
- else
- return(!succes);
- }
-